package test.luin.pers.myapplication;

import android.content.Context;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

public class SaveFile {
    public static boolean SaveFileInfor(Context context, String username, String password) {
        try {
            FileOutputStream fos = context.openFileOutput("data.txt", Context.MODE_PRIVATE);
            fos.write((username + ":" + password).getBytes());
            fos.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static Map<String, String> GetUserInfor(Context context) {
        String content = "";
        try {
            FileInputStream fis = context.openFileInput("data.txt");
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            content = new String(buffer);
            String[] infors = content.split(":");
            Map<String, String> map = new HashMap<>();
            map.put("username", infors[0]);
            map.put("password", infors[1]);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
